package com.hngy.xpq.shoppingcartmodule.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import com.hngy.xpq.shoppingcartmodule.bean.Cart;
import com.hngy.xpq.shoppingcartmodule.database.entity.CartOrder;

import java.util.ArrayList;
import java.util.List;

public class SqlCart extends SQLiteOpenHelper {

    SQLiteDatabase sqLiteDatabase;
    private SqlShop sqlShop;

    public SqlCart(@Nullable Context context) {
        super(context, CartOrder.DATABASE_DATA, null, CartOrder.DATABASE_INS);
        sqLiteDatabase = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + CartOrder.DATABASE_TABLE + "("
                + CartOrder.NOTEPAD_ID + " integer primary key autoincrement,"
                + CartOrder.NOTEPAD_SHOP_ID + " integer,"
                + CartOrder.NOTEPAD_NUMBER + " integer)");
    }

    /**
     * 增
     * @param c
     * @return
     */
    public boolean insert(Cart c) {
        ContentValues values = new ContentValues();
        values.put(CartOrder.NOTEPAD_SHOP_ID, c.getShop_id());
        values.put(CartOrder.NOTEPAD_NUMBER, c.getNumber());
        return
                sqLiteDatabase.insert(CartOrder.DATABASE_TABLE, null, values) > 0;
    }

    /**
     * 改
     * @param c
     * @return
     */
    public boolean update(Cart c) {
        ContentValues values = new ContentValues();
        values.put(CartOrder.NOTEPAD_NUMBER, c.getNumber());
        String sql = CartOrder.NOTEPAD_ID + "=?";
        String[] id = new String[]{String.valueOf(c.getId())};
        return
                sqLiteDatabase.update(CartOrder.DATABASE_TABLE, values, sql, id) > 0;
    }


    /**
     * 删
     * @param id
     * @return
     */
    public boolean delete(int id) {
        String sql = CartOrder.NOTEPAD_ID + "=?";
        String[] va = new String[]{String.valueOf(id)};
        return
                sqLiteDatabase.delete(CartOrder.DATABASE_TABLE, sql, va) > 0;
    }

    public void delete() {
        sqLiteDatabase.execSQL("delete from " + CartOrder.DATABASE_TABLE);
    }

    /**
     * 查
     * @return
     */
    public List<Cart> select(View v) { // 全表遍历
        sqlShop = new SqlShop(v.getContext());
        List<Cart> list = new ArrayList<Cart>();
        try {
            Cursor cursor = sqLiteDatabase.rawQuery("select * from " + CartOrder.DATABASE_TABLE,
                    null, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    Cart d = new Cart();
                    String id = cursor.getString(cursor.getColumnIndex(CartOrder.NOTEPAD_ID));
                    int ids = Integer.valueOf(id);
                    int shop_id = cursor.getInt(cursor.getColumnIndex(CartOrder.NOTEPAD_SHOP_ID));
                    d.setId(ids);
                    d.setShop_id(shop_id);
                    d.setNumber(cursor.getInt(cursor.getColumnIndex(CartOrder.NOTEPAD_NUMBER)));
                    d.setShop(sqlShop.select(shop_id));
                    list.add(d);
                }
                cursor.close();
            } else {
                Log.i("SqlCart-select", "获取Data的信息出现问题!!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public List<Cart> select(View v, int sid) { // 全表遍历
        sqlShop = new SqlShop(v.getContext());
        List<Cart> list = new ArrayList<Cart>();
        try {
            Cursor cursor = sqLiteDatabase.rawQuery("select * from " + CartOrder.DATABASE_TABLE +
                            " where " + CartOrder.NOTEPAD_SHOP_ID + "=" + sid,
                    null,null );
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    Cart d = new Cart();
                    String id = cursor.getString(cursor.getColumnIndex(CartOrder.NOTEPAD_ID));
                    int ids = Integer.valueOf(id);
                    int shop_id = cursor.getInt(cursor.getColumnIndex(CartOrder.NOTEPAD_SHOP_ID));
                    d.setId(ids);
                    d.setShop_id(shop_id);
                    d.setNumber(cursor.getInt(cursor.getColumnIndex(CartOrder.NOTEPAD_NUMBER)));
                    d.setShop(sqlShop.select(shop_id));
                    list.add(d);
                }
                cursor.close();
            } else {
                Log.i("SqlCart-select", "获取Data的信息出现问题!!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

}
